package com.demo;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;

import java.util.Objects;

public class PersonFactoryBean implements FactoryBean<Person> {
    /**
     * Initialize Str
     */
    private String initStr;

    public String getInitStr() {
        return initStr;
    }

    public void setInitStr(String initStr) {
        this.initStr = initStr;
    }

    @Nullable
    @Override
    public Person getObject() throws Exception {
        System.out.println("----->使用getObject方法");
        //初始化Person对象
        Objects.requireNonNull(initStr);
        String[] split = initStr.split(",");
        Person p=new Person();
        p.setpAge(Integer.parseInt(split[0]));
        p.setpName(split[1]);
        //返回Person对象
        return p;
    }



// 以下做法是错误的
//    @Nullable
//    @Override
//    public Person getObject() throws Exception {
//        return new Person();
//    }

    @Nullable
    @Override
    public Class<?> getObjectType() {
        return Person.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}
